<include file="Public/header"/>
<body class="body-bg">
<form id="pay" action="http://tqr.frontpay.cn/sinochem/order/comfirmpay" method="post">
    <input type="hidden" name="token" id="token">
</form>
<div class="park-header">
    <div class="park-header__title">确认订单</div>
    <div class="park-header__left" onclick="window.location='/Home/Cart/index/';">
        <i class="iconfont icon-fanhui"></i>
    </div>
</div>
<div class="container mb-footer">
    <!-- 地址 -->
       <if condition="$address[0]['id']">
         <div class="park-order-address" id="check_address" data-price="{$address[0].scope_price}" data-id="{$address[0].id}" onclick="check_address();">
            <div class="park-order-address__name">{$address[0].name}<span>{$address[0].mobile}</span></div>
            <div class="park-order-address__address">{$address[0].address_area}{$address[0].address}</div>
            <div class="park-order-address__more">
                <i class="iconfont icon-gengduo"></i>
            </div>
         </div>
       <else />
          <div class="park-order-address" style="height:50px;" onclick="window.location='/Home/User/add_address/act/add/';">
            <div class="park-order-address__name">添加新地址<span>{$address.mobile}</span></div>
            <div class="park-order-address__more">
                <i class="iconfont icon-gengduo"></i>
            </div>
          </div>
       </if>
    <!-- 商品 -->
    <div class="park-cart-store-list">
        <volist name="store" id="val">
        <div class="park-cart-store-item">
            <div class="park-cart-store-item__title">
                <div class="left">
                    <i class="iconfont icon-dian"></i>
                    &nbsp;&nbsp;
                    <span>{$val.store_name}</span>
                </div>
            </div>
            <div class="park__goods">
                <ul>
                    <volist name="val['cart_list']" id="vo">
                    <li>
                        <div class="park-goods__pic" style="background-image: url('{$vo.thumb}')"></div>
                        <div class="park-goods__info">
                            <p class="name">{$vo.goods_name}</p>
                            <p class="desc">{$vo.keywords}</p>
                            <p class="spec">规格：{$vo.spec_name}</p>
                            <p class="price-line">
                                <span class="price">￥<em>{:intval($vo['spec_price'])}</em>{:substr($vo['spec_price'],-3,3)}</span>
                            </p>
                        </div>
                        <div class="park-goods__pos">
                            <div class="park-goods-count">
                                <span>X{$vo.goods_number}</span>
                            </div>
                        </div>
                    </li>
                    </volist>
                </ul>
            </div>
        </div>
        </volist>
    </div>
    <!-- 订单设置 -->
    <div class="park-order-setting">
        <div class="park-row-list">
            <div id="coupon-group" class="park-row-item">
                <div class="park-row-item__left">
                    <span>优惠券</span>
                </div>
                <div class="park-row-item__right">
                    <span data-id="0" data-price="0" id="coupon" <if condition="$coupon_number gt 0">onclick="get_coupon();"</if>><if condition="$coupon_number gt 0">{$coupon_number}张可用优惠券<else />无</if></span><i class="iconfont icon-gengduo"></i>
                </div>
            </div>
            <div class="park-row-item">
                <div class="park-row-item__left">
                    <span>支付方式</span>
                </div>
                <div class="park-row-item__right">
                    <div class="park-order-setting__radio">
                        <label class="park-form">
                            <input class="park-form__radio pay_type" type="radio" name="pay_type" checked>
                            <span class="park-form__radioInput"></span>
                            <span class="park-form__text">微信支付</span>
                        </label>
                    </div>
                    <div class="park-order-setting__radio">
                        <label class="park-form">
                            <input class="park-form__radio pay_type" type="radio" name="pay_type">
                            <span class="park-form__radioInput"></span>
                            <span class="park-form__text">余额支付</span>
                        </label>
                    </div>
                </div>
            </div>
            <div class="park-row-item">
                <div class="park-row-item__left">
                    <span>配送方式</span>
                </div>
                <div class="park-row-item__right">
                    <div class="park-order-setting__radio">
                        <label class="park-form">
                            <input class="park-form__radio" checked type="radio">
                            <span class="park-form__radioInput"></span>
                            <span class="park-form__text">达达配送</span>
                        </label>
                    </div>
                </div>
            </div>
            <div class="park-row-item">
                <div class="park-row-item__left">
                    <span>订单备注:</span>
                </div>
                <div class="park-row-item__right">
                    <input type="text" id="order_remark" placeholder="备注点什么" maxlength="18">
                </div>
            </div>
        </div>
    </div>
    <!-- 订单总和 -->
    <div class="park-order-total">
        <p>商品总额：{$price}元</p>
        <p id="fee_price">配送费用：{$scope_price}元</p>
        <p id="use_coupon">使用优惠券：0.00元</p>
        <p>优惠活动：<if condition="$pro_price gt 0">{$pro_price}<else />0.00</if>元</p>
    </div>
</div>
<div id="pay-modal" class="pay-modal">
    <div class="pay-modal-container">
        <div class="pay-modal-header">
            <i class="iconfont icon-del close"></i>
            <span class="title">请输入支付密码</span>
        </div>
        <div class="pay-modal-body">
            <form id="pay-form">
                <div class="pay-modal-body__title">余额支付</div>
                <div class="pay-modal-body__input">
                    <input id="pay-input" type="password" maxlength="6" onkeyup="check_pwd(this);" placeholder="请输入六位数密码" onkeyup="this.value = this.value.replace(/[^\d]/g, '')">
                </div>
            </form>
        </div>
    </div>
</div>
<div class="park-footer">
    <div class="park-confirmOrder-footer">
        <div class="park-confirmOrder-footer-total"><span class="text">总计：</span><span class="price" id="count_price">￥<span>{:intval($count_price)}</span>{:substr($count_price,-3,3)}</span></div>
        <div class="park-confirmOrder-footer-submit" onclick="sub_pay();">立即支付</div>
    </div>
</div>
<div class="park-mask"></div>
<script src="__ROOT__/Public/Home/layer/layer.js"></script>
<script>
    window.pay_id = true;
    $(function () {
        var Coupon = {
            el: $('#coupon'),
            showCoupon : function () {
                this.el.show();
            },
            hideCoupon : function () {
                this.el.hide();
            },
            HandleEvents : function () {
                var _this = this;
                $(document).on('click', '#coupon-group', function () {
                    _this.showCoupon();
                    PARK.showMask();
                });
                $(document).on('click', '.park-mask', function () {
                    _this.hideCoupon();
                });
                $(document).on('click', '#coupon-close', function () {
                    _this.hideCoupon();
                    PARK.hideMask();
                });
            },
            init : function () {
                this.HandleEvents();
            }
        };
		$('.close').click(function(){
			$('#pay-modal').hide();
		});
		pushHistory(); 
	    window.addEventListener("popstate", function(e) { 
		 	window.location = '/Home/Cart/immediately/order_id/'+order_id;
	    }, false); 
	    function pushHistory() { 
		 	var state = { 
				title: "title", 
				url: "#"
		 	}; 
		 	window.history.pushState(state, "title", "#"); 
	    }
        //Coupon.init();
    });
    function sub_pay(){
        var cart_id = '{$cart_id}';
        var address_id = $('#check_address').attr('data-id');
        var order_remark = $('#order_remark').val();
        var coupon_id = $('#coupon').attr('data-id');
        var pay_type = $('.pay_type:checked').next().next().html();
        var pwd = $('#pay-input').val();
        if(pay_type=='余额支付'){
            if(pay_id) {
                $('#pay-modal').show();
                return false;
            }
        }
        if(!address_id){
            layer.open({
                content:'请选择收货地址'
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
            return false;
        }
        $.post('/Home/Cart/pay_order/',{cart_id:cart_id,address_id:address_id,order_remark:order_remark,coupon_id:coupon_id,pay_type:pay_type,pwd:pwd},function(data){
            if(data.code==200) {
				window.order_id = data.data.order_id;
				if(pay_type=='微信支付'){
					$('#token').val(data.data.token);
					$('#pay').submit();
				}else{
                	window.location = '/Home/Cart/immediately/order_id/'+data.data.order_id;
				}
            }else if(data.code==201){
                window.location = '/Home/User/order/';
            }else{
                layer.open({
                    content:data.msg
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        },'json');
    }
    function check_address(){
        var html = '<div class="container mb-footer" style="max-height:500px;overflow-y:auto;">';
        html+='<div style="text-align:center;padding:15px 0;font-size:16px;border-bottom:1px solid #e0e0e0;margin-top:-10px;">选择收货地址</div>';
        <volist name="address" id="val">
            html+='<div class="park-order-address" onclick="click_address(this);" data-price="{$val.scope_price}" data-id="{$val.id}">';
            html+='    <div class="park-order-address__name">{$val.name}<span>{$val.mobile}</span></div>';
            html+='    <div class="park-order-address__address">{$val.address_area}{$val.address}</div>';
            html+='    <div class="park-order-address__more">';
            html+='        <i class="iconfont icon-gengduo"></i>';
            html+='    </div>';
            html+='</div>';
        </volist>
        html+='</div>';
        layer.open({
            type: 1
            ,content: html
            ,anim: 'up'
            ,style: 'position:fixed; bottom:0; left:0; width: 100%; height:{:count($address)*110}px; padding:10px 0; border:none;'
        });
    }
    function click_address(obj){
        $('#check_address').html($(obj).html());
        $('#check_address').attr('data-id',$(obj).attr('data-id'));
        $('#check_address').attr('data-price',$(obj).attr('data-price'));
        $('#fee_price').html('配送费用：'+$(obj).attr('data-price')+'元');
        count_price();
        layer.closeAll();
    }
    //选择优惠券
    function get_coupon(){
        var html = '';
        html+='<div class="park-header">';
        html+='    <div class="park-header__title">使用优惠券</div>';
        html+='    <div class="park-header__left" onclick="close_coupon();">';
        html+='        <i class="iconfont icon-fanhui"></i>';
        html+='    </div>';
        html+='</div>';
        html+='<div class="container">';
        html+='    <div class="park-coupon-unUse" onclick="no_coupon();">';
        html+='        <div class="park-btn park-btn-default park-btn-bottom park-btn-gary">不使用优惠券</div>';
        html+='     </div>';
        html+='     <div class="park-coupon-list">';
        <volist name="coupon_list" id="val">
        html+='         <div class="park-coupon-item" onclick="check_coupon({$val.id},\'{$val.coupon_price}\');">';
        html+='             <div class="park-coupon-info park-coupon-info--1">';
        html+='                 <span class="price">￥<b>{$val.coupon_price}</b></span>';
        html+='                 <span class="price-max">{$val.title}</span>';
        html+='             </div>';
        html+='             <div class="park-coupon-footer">';
        html+='                 <span>有效期:&nbsp;{$val.end_time|date="Y-m-d",###}</span>';
        html+='                 <span>全场通用</span>';
        html+='             </div>';
        html+='         </div>';
        </volist>
        html+='     </div>';
        html+='</div>';
        layer.open({
            type: 1
            ,content: html
            ,anim: 'up'
            ,style: 'position:fixed; bottom:0; left:0; width: 100%; height:100%;border:none;'
        });
    }
    function check_coupon(id,price){
        $('#coupon').html(price+'元优惠券').attr('data-id',id).attr('data-price',price);
        $('#use_coupon').html('使用优惠券：'+price+'元');
        count_price();
        layer.closeAll();
    }
    function no_coupon(){
        $('#coupon').html('不使用优惠券').attr('data-id','').attr('data-price','');
        $('#use_coupon').html('使用优惠券：0.00元');
        count_price();
        layer.closeAll();
    }
    function close_coupon(){
        layer.closeAll();
    }
    function count_price(){
        var price = '{$count_price}';
        var coupon_price = $('#coupon').attr('data-price');
        var pro_price = accSub(price,coupon_price,1);
        var fee_price = $('#check_address').attr('data-price');
        var now_price = accSub(pro_price,fee_price,2);
        $('#count_price').html('￥<span>'+parseInt(now_price)+'</span>'+now_price.substr(now_price.length-3)+'</span>');
    }
    function accSub(arg1,arg2,type){
        var r1,r2,m,n;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2));
        //last modify by deeka
        //动态控制精度长度
        n=(r1>=r2)?r1:r2;
        if(type==1){
            return ((arg1*m-arg2*m)/m).toFixed(2);
        }else{
            return ((arg1*m+arg2*m)/m).toFixed(2);
        }
    }
    //判断支付密码是否正确
    function check_pwd(obj){
        var number = $(obj).val();
        if(number.length==6) {
			$('#pay-input').blur();
            $.post('/Home/Cart/pay_type/',{pwd:number},function(data){
                if(data.code==100){
                    $(obj).val('');
                    layer.open({
                        content:data.msg
                        ,skin: 'msg'
                        ,time: 2 //2秒后自动关闭
                    });
                }else if(data.code==200){
                    window.pay_id = false;
                    sub_pay();
                }
            },'json');
            $('#pay-modal').hide();
        }
    }
</script>
</body>
</html>